#include "TreeList.h"

namespace DuiLib
{
	CNode::CNode(bool bRoot):
		m_pOwner(NULL)
			,m_pRoot(NULL)
			,m_pItem(NULL)

	{
		_tcscpy_s(m_szLabel,TREELIST_LABEL_SIZE,_T("")); 
		if (bRoot)
		{
			m_pItem = new CListUI;	
		}
		else
		{
			m_pItem = new CListContainerElementUI;
		}
	}

	CNode::~CNode()
	{
		if (m_pItem)
		{
			delete m_pItem;
		}
	}



	CControlUI* CNode::GetObject()
	{
		return m_pItem;
	}

	int CNode::ChildNum()
	{
		return m_aChildren.size() ;
	}

	void CNode::AddChild(CNode* pChild)
	{
		m_aChildren.push_back(pChild);
	}

	void CNode::RemoveChild(CNode* pChild)
	{
		delete m_aChildren.back();
		m_aChildren.pop_back();
	}

	CNode* CNode::GetChild(int nIndex)
	{
		return m_aChildren.at(nIndex);
	}

	int CNode::GetCount()
	{
		return m_aChildren.size();
	}

	bool CNode::IsHasChild(CNode* pChild)
	{
		if (m_aChildren.size() > 0)
		{
			return true;
		}
		return false;
	}

	void CNode::Expand()
	{

	}

	void CNode::Hide()
	{

	}

	void CNode::SetOwner(CNode* pOwner)
	{
		m_pOwner = pOwner;
	}

	void CNode::SetLabel(TCHAR* szText)
	{
		_tcscpy_s(m_szLabel,TREELIST_LABEL_SIZE,szText);
	}

	void CNode::GetLabel(TCHAR* lpText,int nLength)
	{
		_tcscpy_s(lpText ,nLength ,m_szLabel);
	}




	CTreeListUI::CTreeListUI()
	{
		m_pRoot = new CNode(true);
	}

	CTreeListUI::~CTreeListUI()
	{
			

	}

	CNode* CTreeListUI::GetNode(int nIndex)
	{
		return m_aChildren[nIndex];	
	}

	bool CTreeListUI::IsHasChild(CNode* pChild)
	{
		return pChild->IsHasChild(pChild);
	}

	void CTreeListUI::AddNode(CNode* pParent ,CNode* pNode)
	{
		//m_List.Add(pNode->GetObject());
		//pNode->SetOwner(m_List);
	}

	void CTreeListUI::DelNode(CNode* pParent ,CNode* pNode)
	{
		
	}

	void CTreeListUI::DoEvent(TEventUI& event) 
	{
		if (!IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND)
		{
			//if (m_pParent != NULL)
			//	m_pParent->DoEvent(event);
			//else
			//	CVerticalLayoutUI::DoEvent(event);
			//return;
		}

		if (event.Type == UIEVENT_TIMER /*&& event.wParam == SCROLL_TIMERID*/)
		{
			//if (delay_left_ > 0)
			//{
			//	--delay_left_;
			//	SIZE sz = GetScrollPos();
			//	LONG lDeltaY =  (LONG)(CalculateDelay((double)delay_left_ / delay_number_) * delay_deltaY_);
			//	if ((lDeltaY > 0 && sz.cy != 0)  || (lDeltaY < 0 && sz.cy != GetScrollRange().cy ))
			//	{
			//		sz.cy -= lDeltaY;
			//		SetScrollPos(sz);
			//		return;
			//	}
			//}
			//delay_deltaY_ = 0;
			//delay_number_ = 0;
			//delay_left_ = 0;
			//m_pManager->KillTimer(this, SCROLL_TIMERID);
			//return;
		}
		if (event.Type == UIEVENT_SCROLLWHEEL)
		{
			//LONG lDeltaY = 0;
			//if (delay_number_ > 0)
			//	lDeltaY =  (LONG)(CalculateDelay((double)delay_left_ / delay_number_) * delay_deltaY_);
			//switch (LOWORD(event.wParam))
			//{
			//case SB_LINEUP:
			//	if (delay_deltaY_ >= 0)
			//		delay_deltaY_ = lDeltaY + 8;
			//	else
			//		delay_deltaY_ = lDeltaY + 12;
			//	break;
			//case SB_LINEDOWN:
			//	if (delay_deltaY_ <= 0)
			//		delay_deltaY_ = lDeltaY - 8;
			//	else
			//		delay_deltaY_ = lDeltaY - 12;
			//	break;
			//}
			//if
			//	(delay_deltaY_ > 100) delay_deltaY_ = 100;
			//else if
			//	(delay_deltaY_ < -100) delay_deltaY_ = -100;

			//delay_number_ = (DWORD)sqrt((double)abs(delay_deltaY_)) * 5;
			//delay_left_ = delay_number_;
			//m_pManager->SetTimer(this, SCROLL_TIMERID, 50U);
			//return;
		}

		CListUI::DoEvent(event);
	}

}